package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.DoctorServicePriceDao;
import com.ym.medical.domain.entity.DoctorServicePriceEntity;
import com.ym.medical.domain.param.DoctorServicePriceQueryParam;
import com.ym.medical.domain.param.DoctorServicePriceUpdateParam;
import com.ym.medical.domain.vo.DoctorServicePriceListPcVO;
import com.ym.medical.domain.vo.DoctorServicePriceVo;
import com.ym.medical.enums.DoctorServiceItemEnum;
import com.ym.medical.service.IDoctorServicePriceSV;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
                                                                            
/**
* 医生问诊规则Service业务层处理
*
* @author ym
* @date 2020-09-14
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class DoctorServicePriceSVImpl extends BaseSVImpl<DoctorServicePriceEntity,DoctorServicePriceDao> implements IDoctorServicePriceSV {

	/**
	 * 查询医生问诊规则列表
	 *
	 * @param queryParam 医生问诊规则 查询参数
	 * @return 医生问诊规则
	 */
	@Override
	public List<DoctorServicePriceListPcVO> selectListPC(DoctorServicePriceQueryParam queryParam) {
		List<DoctorServicePriceListPcVO> doctorServicePrices = mapper.selectListPC(queryParam);
		return doctorServicePrices;
	}

	/**
	 * 新增医生问诊规则
	 *
	 * @param updateParam 医生问诊规则 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(DoctorServicePriceUpdateParam updateParam) {
            DoctorServicePriceEntity entity = updateParam.covert();
		this.insert(entity);
		return entity.getId();
	}

	/**
	 * 修改医生问诊规则
	 *
	 * @param updateParam 医生问诊规则 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(DoctorServicePriceUpdateParam updateParam) {
            DoctorServicePriceEntity entity = updateParam.covert("createTime");
		int result = this.updateById(entity);
		return result;
	}
	/**
	 * 义诊内容
	 * @param doctId
	 * @return
	 */
	@Override
	public DoctorServicePriceVo freeClinicPrice(Long doctId) {
		List<DoctorServicePriceVo> priceList = mapper.freeClinicPrice(doctId,null,"id desc",null);
		DoctorServicePriceVo price = null;
		if(!priceList.isEmpty()){
			price = priceList.get(0);
		}
		//1.1 刚提交
		boolean bool01 = price != null
				&& price.getValidTime().getTime() > System.currentTimeMillis();
		//1.2 已经生效
		boolean bool02 = price != null && FlagEnum.IS.getKey().equals(price.getState());
		//1.3失效
		boolean bool03 = price != null && (price.getInvalidTime().getTime() < System.currentTimeMillis() || price.getLastManTime() == 0);
		if (bool01){
			price.setServiceState(DoctorServiceItemEnum.BY_INQUIRING.getKey());
		}else if(bool02){
			price.setServiceState(DoctorServiceItemEnum.CALL_PHYSICIAN_VISITS.getKey());
		}else if(bool03){
			price.setServiceState(DoctorServiceItemEnum.PRIVATE_DOCTORS.getKey());
		}
		return price;
	}

	/**
	 * 义诊列表
	 * @param doctId
	 * @return
	 */
	@Override
	public PageInfo<DoctorServicePriceVo> freeClinicList(int pageNo, int pageSize, Long doctId) {
		PageHelper.startPage(pageNo,pageSize);
		List<DoctorServicePriceVo> freeClinicList= mapper.freeClinicPrice(doctId,null,null,null);
		return new PageInfo<>(freeClinicList);
	}

	/**
	 * 医生图文生效单价
	 * @param doctId
	 * @return
	 */
	@Override
	public DoctorServicePriceVo effectivePrice(Long doctId) {
		DoctorServicePriceVo price = mapper.effectivePrice(doctId);
		return price;
	}
}
